import { UITransform, Node, Layers, SpriteFrame } from "cc";

export const createUINode = (name: string = '') => {
    const node = new Node(name);

    const transform = node.addComponent(UITransform);
    transform.setAnchorPoint(0, 1);
    node.layer = 1 << Layers.nameToLayer("UI_2D");
    return node;
}

/** 生成唯一ID */
export const randomById = (id: number) => {
    return Array.from({ length: id }).reduce<string>((total, item) => total + Math.floor(Math.random() * 10), '')
}

/** 获取随机数 */
export const getRandomInt = (min: number, max: number) => {
    return Math.floor(Math.random() * (max - min)) + min;
}

const reg = /\((\d+)\)/

const getNum = (str: string) => parseInt(str.match(reg)[1] || "0")

/** 排序精灵帧 */
export const sortSpriteFrame = (spriteFrames: SpriteFrame[]) => {
    return spriteFrames.sort((a, b) => getNum(a.name) - getNum(b.name))
}

